//
//  HNSH_MineImageAlert.m
//  HNSH_SingleProject
//
//  Created by 李宇廷 on 2023/3/10.
//

#import "HNSH_MineImageAlert.h"
#import "HNSH_PopupController.h"

#import "KKImageBrowser.h"

@interface HNSH_MineImageAlert ()
/* nyPopView */
@property(nonatomic, strong) HNSH_PopupController *nyPopView;

/* titleLabel */
@property (nonatomic, strong) UILabel *titleLabel;
/* desLabel */
@property (nonatomic, strong) UILabel *desLabel;
/* closeButton */
@property (nonatomic, strong) UIButton *closeButton;
/* imgV */
@property (nonatomic, strong) UIImageView *imgV;

@end

@implementation HNSH_MineImageAlert

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = [UIColor whiteColor];
        [self configUI];
    }
    return self;
}

- (void)showView {
    self.nyPopView = [[HNSH_PopupController alloc]init];
    self.nyPopView.dismissOnMaskTouched = YES;
    self.nyPopView.layoutType = PopupLayoutTypeCenter;
    self.nyPopView.slideStyle = PopupSlideStyleFade;
    [self.nyPopView presentContentView:self duration:0.25 springAnimated:YES inView:kSTWindow];
    self.nyPopView.popupView.layer.cornerRadius = 16.0f;
    self.nyPopView.popupView.layer.masksToBounds = YES;
}

- (void)setImgString:(NSString *)imgString {
    _imgString = imgString;
    [self.imgV sd_setImageWithURL:[NSURL URLWithString:imgString]];
}

- (void)closeView {
    [self.nyPopView dismissWithDuration:0.25 springAnimated:YES];
}

- (void)configUI {
    [self addSubview:self.titleLabel];
    [self addSubview:self.desLabel];
    [self addSubview:self.closeButton];
    [self addSubview:self.imgV];
    
    [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.mas_centerX);
        make.top.mas_equalTo(16);
        make.height.mas_equalTo(22);
    }];
    
    [self.desLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(46);
        make.centerX.equalTo(self.mas_centerX);
        make.height.mas_equalTo(20);
    }];
    
    [self.closeButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(16);
        make.right.bottom.mas_equalTo(-16);
        make.height.mas_equalTo(40);
    }];
}

#pragma mark lazy
- (UILabel *)titleLabel {
    if (!_titleLabel) {
        _titleLabel = [[UILabel alloc]init];
        _titleLabel.textColor = [UIColor appBlack333LabelColor];
        _titleLabel.font = [UIFont boldSystemFontOfSize:16];
        _titleLabel.text = @"查看示例";
    }
    return _titleLabel;
}

- (UILabel *)desLabel {
    if (!_desLabel) {
        _desLabel = [[UILabel alloc]init];
        _desLabel.textColor = [UIColor colorWithHexString:@"#999999"];
        _desLabel.font = [UIFont systemFontOfSize:14];
        _desLabel.numberOfLines = 0;
        _desLabel.text = @"点击查看大图";
    }
    return _desLabel;
}

- (UIImageView *)imgV {
    if (!_imgV) {
        _imgV = [[UIImageView alloc]initWithFrame:CGRectMake(33, 73, self.width-66, 140)];
        _imgV.contentMode = UIViewContentModeScaleAspectFill;
        _imgV.clipsToBounds = YES;
        _imgV.userInteractionEnabled = YES;
        WeakSelf(weakSelf);
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithActionBlock:^(id  _Nonnull sender) {
            KKImageBrowserModel *model = [[KKImageBrowserModel alloc] initWithURL:[NSURL URLWithString:weakSelf.imgString] toView:kSTWindow];
            KKImageBrowser *vc = [[KKImageBrowser alloc] init];
            vc.images = @[model];
            vc.index = 0;
            [[UIViewControllerHelper findCurrentShowingViewController] presentViewController:vc animated:NO completion:nil];
        }];
        [_imgV addGestureRecognizer:tap];
    }
    return _imgV;
}

- (UIButton *)closeButton {
    if (!_closeButton) {
        _closeButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [_closeButton setTitle:@"返回" forState:UIControlStateNormal];
        [_closeButton setTitleColor:[UIColor appWhiteLabelColor] forState:UIControlStateNormal];
        _closeButton.backgroundColor = [UIColor appGreenColor];
        _closeButton.layer.cornerRadius = 20.0f;
        _closeButton.layer.masksToBounds = YES;
        __weak __typeof(&*self)weakSelf = self;
        [[_closeButton rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(__kindof UIControl * _Nullable x) {
            [weakSelf closeView];
        }];
    }
    return _closeButton;
}


@end
